const koa = require("koa");
const app = new koa();
const koaBody = require("koa-body");
const router = require("koa-router")();
const cors = require("koa2-cors");

router.post("/login", async ctx => {
    var { username, pass } = ctx.request.body;
    console.log(username, pass);
    if (username == "cheng" && pass == "123456") {
        ctx.cookies.set("loginAuth", true, {
            httpOnly: false
        });
        ctx.body = {
            code: 200,
            msg: "登陆成功"
        }
    } else {
        ctx.body = {
            code: 400,
            msg: "登陆失败,用户名或密码错误"
        }
    }
})

// 配置后端cookie可以访问
app.use(cors({
    origin: ctx => {
        return ctx.headers.origin
    },
    credentials: true
}));
app.use(koaBody());
app.use(router.routes());

app.listen(8000);